local path = require('config.path')

local packer = nil

local function loadPacker()
  packer = require('packer')
  packer.init {
    package_root = path.data_path..'/pack',
    git = { clone_timeout = 120 },
    display = {
      open_fn = function ()
        return require('packer.util').float {border="single"}
      end,
      prompt_border = "single",
    },
    profile = {
      enable = true,
      threshold = 1,
    },
    auto_clean = true,
    compile_on_sync = true,
  }
end

local function loadPlugins()
  local plugins = require('plugins')
  packer.startup(function(use)
    for _, plugin in ipairs(plugins) do
      use(plugin)
    end
  end)
end

local function setup()
  local install_path = path.data_path..'/pack/packer/start/packer.nvim'
  if vim.fn.empty(vim.fn.glob(install_path)) > 0 then
    vim.fn.system {'git', 'clone', 'https://github.com/wbthomason/packer.nvim', '--depth', '1', install_path}
    vim.cmd [[packadd packer.nvim]]
    loadPacker()
    loadPlugins()
    packer.install()
    packer.compile()
  else
    loadPacker()
    loadPlugins()
  end
end

return {
  setup = setup,
}
